package im.elephas.androfan;

import im.elephas.fanfouapi.FanfouApi;
import im.elephas.fanfouapi.events.IErrorEvent;
import im.elephas.fanfouapi.events.IUpdateEvent;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class SendMessageActivity extends Activity implements OnClickListener,
		TextWatcher {

	private String msg;

	private boolean isReply = false;
	private String atName = "";
	private String replyId = "";
	
	private ImageButton button;
	private EditText et;
	private TextView tv;

	private FanfouApi mFanfouApi;
	private SendMessageActivity mSendMessageActivity = this;

	private IUpdateEvent mIUpdateEvent = new IUpdateEvent() {

		@Override
		public void updateHandler(String result) {
			mSendMessageActivity.finish();
		}
	};

	private IErrorEvent mIErrorEvent = new IErrorEvent() {

		@Override
		public void errorHandler() {
			Builder dialog = new AlertDialog.Builder(mSendMessageActivity);
			dialog.setTitle("获取消息失败！请检查网络设置。");
			dialog.show();
			mSendMessageActivity.finish();
		}
	};

	@Override
	public void afterTextChanged(Editable s) {
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
	}

	@Override
	public void onClick(View v) {
		if (v.equals(button)) {
			String status = et.getText().toString();
			if (isReply) {
				mFanfouApi.replyMessage(status, replyId, mIUpdateEvent,
						mIErrorEvent);
			} else {
				mFanfouApi.updateMessage(status, mIUpdateEvent, mIErrorEvent);
			}
			et.setEnabled(false);
			button.setEnabled(false);
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newmessage);
		setTitle("你在做什么？");
	
		try {
			isReply = getIntent().getExtras().getBoolean("isreply");
		} catch (Exception e) {
			isReply = false;
		}

		mFanfouApi = FanfouApi.getInstance();
		
		et = (EditText) findViewById(R.id.messageEditText);
		button = (ImageButton) findViewById(R.id.sendButton);
		button.setOnClickListener(this);
		button.setEnabled(false);

		if (isReply) {
			atName = getIntent().getExtras().getString("atname");
			replyId = getIntent().getExtras().getString("replyid");
			setTitle("回复@" + atName);
			et.setText("@" + atName + " ");
		}
		et.addTextChangedListener(this);

		tv = (TextView) findViewById(R.id.countTextView);
		int i = et.getText().length();
		tv.setText(Integer.toString(140 - i));
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		int i = et.getText().length();
		if (i <= 140 && i >= 0) {
			button.setEnabled(true);
			tv.setText(Integer.toString(140 - i));
		} else {
			button.setEnabled(false);
			et.setText(msg);
		}
		msg = new String(et.getText().toString());
	}

}
